﻿/**
 * AviTagsWrapper is a wrapper for abcAVI written in C# 
 * Copyright (C) 2010 Chris Meyer
 * 
 * This file is part of AviTagsWrapper.
 * 
 * AviTagsWrapper is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * AviTagsWrapper is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with AviTagsWrapper.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Chris Meyer (chris.meyer@ngsoftonline.com)
 * http://ngsoftonline.com/
 * 
 **/
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using AviTagsWrapper;
using BF = System.Reflection.BindingFlags;

namespace AviTagsWrapperTester {
    public partial class Tester : Form {

        private readonly AviTags tags;

        public Tester() {
            InitializeComponent();

            tags = new AviTags();
            tags.StatusChanged += StatusChanged;
        }

        private void DisplayTags() {

            if(tags != null) {
                if(tags.TagsLoaded) {
                    var nonNullProperties =
                                 typeof(ExtendedInfoTags).GetProperties(BF.Public | BF.DeclaredOnly | BF.Instance)
                                 .Where(p => p.CanRead && !((string)p.GetValue(tags.Tags, null) == ""));

                    lvInfo.Items.Clear();
                    foreach(var info in nonNullProperties) {
                        ListViewItem item = new ListViewItem(info.GetValue(tags.Tags, null).ToString());
                        item.SubItems.Add(info.Name);
                        item.BackColor = Color.LightGreen;
                        lvInfo.Items.Add(item);
                    }
                }
            }
        }

        private void StatusChanged(object sender, StatusEventArgs args) {
            statusLabel.Text = args.Msg;
        }

        private void btLoad_Click(object sender, EventArgs e) {
            lvInfo.Items.Clear();

            if(openFileDialog.ShowDialog() == DialogResult.OK) {
                tags.LoadTags(openFileDialog.FileName);
                DisplayTags();
                deleteTagToolStripMenuItem.Enabled = true;
            }
        }

        private void saveBtn_Click(object sender, EventArgs e) {
            if(tags != null)
                if(!tags.ChangesSaved) {
                    tags.SaveChanges();

                    //reload the tags displayed
                    DisplayTags();
                }

        }

        private void addBtn_Click(object sender, EventArgs e) {
            if(tagValueTxtBox.Text != "" && tagsCbBox.SelectedItem != null) {
                if(tags != null) {
                    tags.SetTag(tagsCbBox.SelectedItem.ToString(), tagValueTxtBox.Text);

                    //add the item to the list view
                    ListViewItem item = new ListViewItem(tagValueTxtBox.Text);
                    item.SubItems.Add(tagsCbBox.SelectedItem.ToString());
                    item.BackColor = Color.Beige;
                    lvInfo.Items.Add(item);

                    //clear out the values
                    tagValueTxtBox.Text = "";
                    tagsCbBox.SelectedIndex = -1;
                }

            }
        }

        private void Form1_Load(object sender, EventArgs e) {
            statusLabel.Text = "";
            tagsCbBox.Items.AddRange(ExtendedInfoTags.GetExtendedInfoTagsPropertyNames().ToArray());
        }

        private void deleteTagToolStripMenuItem_Click(object sender, EventArgs e) {

            if(lvInfo.SelectedItems.Count > 0) {
                string tagName = lvInfo.SelectedItems[0].SubItems[1].Text;
                if(tags != null) {
                    tags.SetTag(tagName, "");
                    lvInfo.SelectedItems[0].BackColor = Color.Red;
                    saveBtn.Focus();
                }

            }
        }

    }
}
